﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace AACalc.ViewModels
{
    public class Sheet : INotifyPropertyChanged
    {
        private string name;
        private DateTime createdTime;
        private DateTime modifiedTime;
        private double total;
        private double realTotal;

        public ObservableCollection<Attendee> Attendees { get; private set; }
        public ObservableCollection<Activity> Activities { get; private set; }

        public Sheet()
        {
            this.createdTime = DateTime.Now;
            this.modifiedTime = DateTime.Now;
            this.Attendees = new ObservableCollection<Attendee>();
            this.Activities = new ObservableCollection<Activity>();

            var aaron = new Attendee { Name = "Aaron" };
            var tony = new Attendee { Name = "Tony" };
            var lara = new Attendee { Name = "Lara" };
            var ricky = new Attendee { Name = "Ricky" };
            var jane = new Attendee { Name = "Jane" };
            var zhirong = new Attendee { Name = "Zhirong Chen" };
            var andy = new Attendee { Name = "Andy Wu" };
            var connie = new Attendee { Name = "Connie" };
            var cai = new Attendee { Name = "小蔡" };

            this.Attendees.Add(aaron);
            this.Attendees.Add(tony);
            this.Attendees.Add(lara);
            this.Attendees.Add(ricky);
            this.Attendees.Add(jane);
            this.Attendees.Add(zhirong);
            this.Attendees.Add(andy);
            this.Attendees.Add(connie);
            this.Attendees.Add(cai);

            this.Activities.Add(new Activity { Name = "Drinks ", Cost = 24, Attendees = { aaron, tony, lara, ricky, jane, zhirong, andy, connie } });
            this.Activities.Add(new Activity { Name = "KTV 1", Cost = 114, Attendees = { aaron, lara, ricky, zhirong, andy, connie } });
            this.Activities.Add(new Activity { Name = "KTV 2", Cost = 232, Attendees = { aaron, lara, ricky, zhirong, andy, connie, tony, jane } });
            this.Activities.Add(new Activity { Name = "Lunch(MacDonald) ", Cost = 100, Attendees = { aaron, lara, ricky, zhirong, andy, connie } });
            this.Activities.Add(new Activity { Name = "Supper ", Cost = 518, Attendees = { aaron, tony, lara, ricky, jane, andy, connie, cai } });

        }

        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                if (value != this.name)
                {
                    this.name = value;
                    this.NotifyPropertyChanged("Name");
                }
            }
        }

        public DateTime CreatedTime
        {
            get
            {
                return this.createdTime;
            }
        }

        public DateTime ModifiedTime
        {
            get
            {
                return this.modifiedTime;
            }
        }

        public double Total
        {
            get
            {
                return this.total;
            }

            private set
            {
                this.total = value;
                this.NotifyPropertyChanged("Total");
            }
        }

        public double RealTotal
        {
            get
            {
                return this.realTotal;
            }

            private set
            {
                this.realTotal = value;
                this.NotifyPropertyChanged("RealTotal");
            }
        }

        public void Calculate()
        {
            this.total = 0;
            this.ClearAttendeesNeedPay();

            foreach (var activity in this.Activities.Where(c => c.Attendees.Count > 0))
            {
                total += activity.Cost;
                var avg = activity.Cost / activity.Attendees.Count;

                foreach (var attendee in activity.Attendees)
                {
                    attendee.NeedPay += avg;
                }
            }

            this.CalculateRealTotal();
        }

        private void CalculateRealTotal()
        {
            this.realTotal = 0;

            foreach (var attendee in this.Attendees)
            {
                attendee.NeedPay = Math.Round(attendee.NeedPay, 0);
                this.realTotal += attendee.NeedPay;
            }
        }

        private void ClearAttendeesNeedPay()
        {
            foreach (var a in this.Attendees)
            {
                a.NeedPay = 0;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String propertyName)
        {
            var handler = PropertyChanged;

            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
